
#include <QTcpSocket>
#include <QByteArray>

#include "applicationtest.h"

ApplicationTest::ApplicationTest(QObject *parent) : QObject(parent)
{
    p_server = new QTcpServer(this);
    p_server->listen(QHostAddress::Any, 5011);
    QObject::connect(p_server, SIGNAL(newConnection()), this, SLOT(connectNewClient()));
}

void ApplicationTest::connectNewClient()
{
    QTcpSocket *socket = p_server->nextPendingConnection();
    qDebug() << "New connected client";
    connect(socket, SIGNAL(readyRead()), this, SLOT(processIncomingData()));
    socket->write(QByteArray("Hello world!"));
}

void ApplicationTest::processIncomingData()
{
    QTcpSocket *socket = static_cast<QTcpSocket*>(sender());
    QByteArray data = socket->readAll();
    qDebug() << "Data " << data;
    socket->write(data);
}
